﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using IAirPlanePlugin;
using System.Web.Mvc;

namespace RutTrichThongTin
{
    public class AirPlaneTicketFeederManager
    {
        private List<IAirPlaneFeeder> _ListAirPlainFeeder;

        private Dictionary<string, int> _CuaHang;

        public Dictionary<string, int> CuaHang
        {
            get { return _CuaHang; }
            set { _CuaHang = value; }
        }
        public AirPlaneTicketFeederManager()
        {
            _ListAirPlainFeeder = new List<IAirPlaneFeeder>();
        }

        public List<string> getListColumn(int idCuaHang)
        {
            if (idCuaHang == -1)
            {
                List<string> listCol = new List<string>();
                listCol.Add("NoiDi");
                listCol.Add("NoiDen");
                listCol.Add("HangMayBay");
                listCol.Add("GiaVe");
                return listCol;
            }
            else
            {
                return _ListAirPlainFeeder[idCuaHang].getListColumnName();
            }
            return null;
        }

        public List<AirPlaneTicketInfo> getTicket(string NoiDi, string NoiDen,string NgayDi,string ThangDi,int idCuaHang)
        {
            
            int n = _ListAirPlainFeeder.Count;

            if (idCuaHang == -1)
            {
                List<AirPlaneTicketInfo> listTicket = new List<AirPlaneTicketInfo>();    
                List<AirPlaneTicketInfo> listTemp;
                for (int i = 0; i < n; i++)
                {
                    listTemp = _ListAirPlainFeeder[i].getTicketInfo(NoiDi, NoiDen, NgayDi, ThangDi, idCuaHang);
                    if (listTemp != null && listTemp.Count > 0)
                    {
                        int m = listTemp.Count;
                        for (int j = 0; j < m; j++)
                        {
                            listTicket.Add(listTemp[j]);
                        }
                    }
                }
                return listTicket;
            }
            else
            {
                if (idCuaHang >= 0 && idCuaHang < n)
                {
                    return _ListAirPlainFeeder[idCuaHang].getTicketInfo(NoiDi, NoiDen, NgayDi, ThangDi, idCuaHang);
                }
            }
            return null;
        }

        public void LoadFeederLibrary(string path)
        {
            _CuaHang = new Dictionary<string, int>();
            _CuaHang.Add("Tất cả", -1);
            int id = 0;

            IAirPlaneFeeder temp = null;
            _ListAirPlainFeeder.Clear();

            string[] files = System.IO.Directory.GetFiles(path + @"\AddOn\", "*.dll");

            foreach (string f in files)
            {                
                Assembly asm = Assembly.LoadFile(f);
                Type[] types = asm.GetTypes();
                foreach (Type t in types)
                {
                    if (typeof(IAirPlaneFeeder).IsAssignableFrom(t) )
                    {
                        temp = Activator.CreateInstance(t) as IAirPlaneFeeder;
                        CuaHang.Add(temp.GetName(),id);
                        id++;
                        _ListAirPlainFeeder.Add(temp);
                    }
                }
            }
        }
    }
}